package UILayer;

import CtrLayer.LeaseCtr;
import java.awt.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
import java.awt.event.*;
public class LeaseFinish extends JInternalFrame {
	private JTextField leaseid_tf;
	private JTextField startdate_tf;
	private JTextField entereddate_tf;
	private JTextField return_tf;
	private JTextField price_tf;
	private JPanel panel_4;
	private String id;
	private LeaseCtr leases;
	private Object[] lease;
	DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	Calendar cal = Calendar.getInstance();

	public LeaseFinish(String id) {
		setTitle("Complete Lease");
		this.id=id;
		leases =new LeaseCtr();
		setClosable(true);
		setBounds(100, 100, 246, 212);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.EAST);
		
		JButton btnNewButton = new JButton("Back");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
	    		dispose();
			}
		});
		panel_2.add(btnNewButton);
		
		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3, BorderLayout.WEST);
		
		JButton btnNewButton_1 = new JButton("Complete");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				save();
			}
		});
		panel_3.add(btnNewButton_1);
		
		panel_4 = new JPanel();
		panel.add(panel_4, BorderLayout.CENTER);
		panel_4.setLayout(null);
		lease = leases.display_lease_id(id);
		make_lbls();
		make_fill_tfs();
	}
	private void make_fill_tfs()
	{

		leaseid_tf = new JTextField(id);
		leaseid_tf.setEnabled(false);
		leaseid_tf.setBounds(119, 8, 86, 20);
		panel_4.add(leaseid_tf);
		leaseid_tf.setColumns(10);
		startdate_tf = new JTextField(String.valueOf(lease[1]));
		startdate_tf.setEnabled(false);
		startdate_tf.setBounds(119, 30, 86, 20);
		panel_4.add(startdate_tf);
		startdate_tf.setColumns(10);
		
		entereddate_tf = new JTextField(String.valueOf(lease[2]));
		entereddate_tf.setEnabled(false);
		entereddate_tf.setBounds(119, 52, 86, 20);
		panel_4.add(entereddate_tf);
		entereddate_tf.setColumns(10);
		
		return_tf = new JTextField(dateFormat.format(cal.getTime()));
		return_tf.setEnabled(false);
		return_tf.setColumns(10);
		return_tf.setBounds(119, 74, 86, 20);
		panel_4.add(return_tf);
		
		price_tf = new JTextField(compare_prices()+"");
		price_tf.setEnabled(false);
		price_tf.setColumns(10);
		price_tf.setBounds(119, 96, 86, 20);
		panel_4.add(price_tf);
	}
	private void make_lbls()
	{
		JLabel lblNewLabel = new JLabel("Lease Date:");
		lblNewLabel.setBounds(10, 29, 58, 22);
		panel_4.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Entered Return Date:");
		lblNewLabel_1.setBounds(10, 51, 104, 22);
		panel_4.add(lblNewLabel_1);
		
		JLabel lblLeaseId = new JLabel("Lease ID:");
		lblLeaseId.setBounds(10, 11, 46, 14);
		panel_4.add(lblLeaseId);
		
		JLabel lblFinalPrice = new JLabel("Final Price:");
		lblFinalPrice.setBounds(10, 95, 104, 22);
		panel_4.add(lblFinalPrice);
		
		JLabel lblActualReturnDate = new JLabel("Actual Return Date:");
		lblActualReturnDate.setBounds(10, 73, 104, 22);
		panel_4.add(lblActualReturnDate);
	}
    private double compare_prices()
    {
    	
    	try
    	{
    		Date give = (Date) dateFormat.parse(String.valueOf(lease[1]));
    		Date exp = (Date) dateFormat.parse(String.valueOf(lease[2]));
    		Date act = new Date();
    		double price;
    		if ((int)Math.ceil((exp.getTime()-give.getTime())/86400000)==0)
    		{
    			price=Double.parseDouble(String.valueOf(lease[3]));
    		}
    		else
    		{
    			price=Double.parseDouble(String.valueOf(lease[3]))/((int)Math.ceil((exp.getTime()-give.getTime())/86400000));
    		}
    		double days = (int)Math.ceil(((act.getTime()-give.getTime())/86400000));
        	if (days==0)
        	{
        		days+=1;
        	}
    		return price*days;
    	}
    	catch (Exception e)
    	{
    		return 0;
    	}
    	
    }
    private void save()
    {
    	try
    	{
    		leases.close_order(id, compare_prices());
    		JOptionPane.showMessageDialog(this, "<html>You successfully completed this lease.</html>", "Success", JOptionPane.INFORMATION_MESSAGE);
    		dispose();
    	}
    	catch(Exception ex)
    	{
    		JOptionPane.showMessageDialog(this, "<html>You did not successfully completed this lease.<br/> There is no such lease.</html>", "Error", JOptionPane.INFORMATION_MESSAGE);
    	}
    }
}
